package com.aster.common.api.service.imp;

import com.aster.common.api.dao.AppTokenDao;
import com.aster.common.api.domain.TbAppToken;
import com.aster.common.api.service.AppTokenService;
import com.aster.common.api.service.criteria.AppTokenCriteria;
import com.aster.common.base.BaseServiceImpl;
import com.aster.common.exception.ServiceException;
import com.aster.common.utils.DateUtil;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.UUID;

/**
 * 
 * <br>
 * <b>功能：</b>TbAppTokenService<br>
 */
@Service("tbAppTokenService")
public class  AppTokenServiceImpl  extends BaseServiceImpl implements AppTokenService {
  private final static Logger log= Logger.getLogger(AppTokenServiceImpl.class);
	

	@Autowired
    private AppTokenDao dao;

		
	public AppTokenDao getDao() {
		return dao;
	}


	@Override
	public boolean overexpiredCheck(String openId) throws ServiceException {
		try {
			TbAppToken tbAppToken = getTbAppToken("openId",openId);
			if(tbAppToken == null){
				return true;
			}
			Long updateTime = DateUtil.nowDateTIme().getTime();
			Long expirTime = tbAppToken.getExpirTime().getTime();
			if(updateTime > expirTime){
				return true;
			}
			return false;
		}catch (ServiceException ex){
			throw ex;
		}catch (Exception ex){
			throw new ServiceException(55555,ex.getMessage());
		}
	}

	@Override
	public String updateToken(String openId) throws ServiceException {
		try {
			TbAppToken tbAppToken = getTbAppToken("openId",openId);
			Date expirTime = new Date();
			Date now = new Date();
			expirTime.setTime(now.getTime()+60*60*1000);
			String tokenId = UUID.randomUUID().toString();
			if(tbAppToken == null){
				tbAppToken = new TbAppToken();
				tbAppToken.setOpenId(openId);
				tbAppToken.setToken(tokenId);
				tbAppToken.setUnionId(openId);
				tbAppToken.setUpdateTime(now);
				tbAppToken.setType(1);
				tbAppToken.setExpirTime(expirTime);
				super.add(tbAppToken);
			}
			TbAppToken updateToken = new TbAppToken();
			updateToken.setOpenId(openId);
			updateToken.setUpdateTime(now);
			updateToken.setToken(tokenId);
			tbAppToken.setExpirTime(expirTime);
			super.updateBySelective(updateToken);
			return tokenId;
		}catch (ServiceException ex){
			throw ex;
		}catch (Exception ex){
			throw new ServiceException(55555,ex.getMessage());
		}
	}

	@Override
	public String getOpenIdByToken(String token) throws ServiceException {
		TbAppToken tbAppToken = getTbAppToken("token",token);
		if(null == tbAppToken){
			return null;
		}
		return tbAppToken.getToken();
	}

	private TbAppToken getTbAppToken(String key,String value) throws ServiceException {
		try {
			AppTokenCriteria criteria = new AppTokenCriteria();
			criteria.put(key,value);
			TbAppToken tbAppToken = dao.getAppToken(criteria);
			return tbAppToken;
		}catch (Exception ex){
			log.error(ex.getMessage());
			throw new ServiceException(55555,ex.getMessage());
		}
	}
}
